package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.WeiboDao;
import com.base.kongjian.entity.Weibo;
import com.base.kongjian.service.WealthRecService;
import com.base.kongjian.service.WeiboService;
import com.base.kongjian.service.WorkShareRecService;
import com.base.kongjian.service.WorksContributeRecService;
import com.base.kongjian.service.WorksNumCountService;
import com.mysteel.page.Page;

@Service("weiboService")
public class WeiboServiceImpl implements WeiboService
{
	@Autowired
	private WorksNumCountService worksNumCountService;
	@Autowired
	private WeiboDao weiboDao;
	/*@Autowired
	private WealthSystemService wealthSystemService;*/
	@Autowired
	private WorkShareRecService workShareRecService;
	/*@Autowired
	private PopularitySystemService popularitySystemService;*/
	@Autowired
	private WorksContributeRecService worksContributeRecService;

	@Autowired
	private WealthRecService wealthRecService;

	@Override
	public void saveWeibo(Weibo weibo)
	{
		Assert.notNull (weibo);
		
		long memberId = weibo.getMemberId ();
		
		worksNumCountService.updateWorksNumCount (memberId ,"weibo" ,"add");
		
		weiboDao.save (weibo);
	}

	@Override
	public Weibo getById(long id)
	{
		Weibo weibo  = weiboDao.get (id);
		
		if(ObjectUtils.notNull (weibo))
		{
			weibo.setBrowseCount (weibo.getBrowseCount ()+1);
			
			weiboDao.update  (weibo);
		}
		
		return weibo;
	}

	@Override
	public void deleteById(long id)
	{
		Weibo weibo = weiboDao.get (id);
		
		if(ObjectUtils.notNull (weibo))
		{
			long memberId =  weibo.getMemberId ();
			
			worksNumCountService.updateWorksNumCount (memberId ,"weibo" ,"delete");
			
			Map<String,Object> params = new HashMap<String,Object>();
			params.put ("workId" ,id);
			params.put ("workKind" ,1);
			
			worksContributeRecService.deleteWCRByMap (params);
			workShareRecService.deleteWSRByMap (params);
			wealthRecService.deleteWRByMap (params);
			weiboDao.delete (id);
		}	
	}

	@Override
	public Page<Weibo> queryWeibo(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<Weibo> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = weiboDao.count(params);

		if (count > 0)
		{
			page = new Page<Weibo>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> weiboIds = weiboDao.query(params);

			if (ObjectUtils.notEmpty(weiboIds))
			{
				List<Weibo> weibos = new ArrayList<Weibo>(weiboIds.size());

				for (long weiboId : weiboIds)
				{
					weibos.add(getById (weiboId));
				}

				page.setDatas(weibos);
			}
		}

		return page;
	}

	@Override
	public void updateWeibo(Weibo weibo)
	{
		Assert.notNull (weibo);
		
		long id = weibo.getId ();	
		Weibo w = weiboDao.get(id);
		if(ObjectUtils.notNull (w))
		{
			w.setBrowseCount (weibo.getBrowseCount ());
			w.setCollectionCount (weibo.getCollectionCount ());
			w.setCommentCount (weibo.getCommentCount ());
			w.setContent (weibo.getContent ());
			w.setMemberId (weibo.getMemberId ());
			w.setPraiseCount (weibo.getPraiseCount ());
			w.setPublishTime (weibo.getPublishTime ());
			w.setTitle (weibo.getTitle ());
			w.setTransmitCount (weibo.getTransmitCount ());
			
			weiboDao.update (w);
		}	
	}

	@Override
	public void addPraiseCount(long id,long sharerId)
	{
		Weibo weibo = getById (id);
		if(ObjectUtils.notNull (weibo))
		{	
			long allCount = getPraiseCount (id);
			
			weibo.setPraiseCount (allCount+1);
			
			
			long memberId = weibo.getMemberId ();
			boolean b = workShareRecService.addShareRec (id ,1 ,3 ,memberId ,sharerId);	
			if(b==true)
			{
				updateWeibo (weibo);
			}
		}
	}

	@Override
	public void addCommentCount(long id,long sharerId)
	{
		Weibo weibo = getById (id);
		if(ObjectUtils.notNull (weibo))
		{
			long allCount = getCommentCount  (id);
			weibo.setCommentCount (allCount+1);
			
			updateWeibo (weibo);
			
			long memberId = weibo.getMemberId ();
			boolean b =workShareRecService.addShareRec (id ,1 ,4 ,memberId ,sharerId);
			
			if(b==true)
			{
				updateWeibo (weibo);
			}
		}	
	}

	@Override
	public void addCollectionCount(long id,long sharerId)
	{
		Weibo weibo = getById (id);
		if(ObjectUtils.notNull (weibo))
		{
			long allCount = getCollectionCount (id);
			weibo.setCollectionCount (allCount+1);
			
			long memberId = weibo.getMemberId ();
			boolean b = workShareRecService.addShareRec (id ,1 ,2 ,memberId ,sharerId);		
			if(b==true)
			{
				updateWeibo (weibo);
			}
		}
	}

	@Override
	public void addTransmitCount(long id,long sharerId)
	{
		Weibo weibo = getById (id);
		if(ObjectUtils.notNull (weibo))
		{
			long allCount = getTransmitCount(id);
			weibo.setTransmitCount (allCount+1);
			
			long memberId = weibo.getMemberId ();
			boolean b = workShareRecService.addShareRec (id ,1 ,1 ,memberId ,sharerId);
			
			if(b==true)
			{
				updateWeibo (weibo);
			}
		}	
	}

	@Override
	public  void addBrowseCount(long id)
	{
		Weibo weibo = getById (id);
		if(ObjectUtils.notNull (weibo))
		{
			long allCount = getBrowseCount (id);
			weibo.setBrowseCount (allCount+1);
			
			updateWeibo (weibo);		
		}	
	}

	@Override
	public synchronized long getPraiseCount(long id)
	{
		Weibo weibo = getById (id);
		
		return weibo.getPraiseCount ();
	}

	@Override
	public synchronized long getCommentCount(long id)
	{
		Weibo weibo = getById (id);
		
		return weibo.getCommentCount ();
	}

	@Override
	public synchronized long getCollectionCount(long id)
	{
		Weibo weibo = getById (id);
		
		return weibo.getCollectionCount ();
	}

	@Override
	public synchronized long getBrowseCount(long id)
	{
		Weibo weibo = getById (id);
		
		return weibo.getBrowseCount ();
	}

	@Override
	public synchronized long getTransmitCount(long id)
	{
		Weibo weibo = getById (id);
		
		return weibo.getTransmitCount ();
	}

}
